import Cesium from '../Ces/Cesium'

class EditablePoint {
  static baseColor
  static highLightColor
  static pixelSize
  static outlineWidth
  static outlineColor

  onPostionChanged
  _extent
  _point
  _removeEventSubscription
  _postionChangedEvent
  _tooltip
  _private = false
  _allowPick = true
  _collection = new Cesium.PointPrimitiveCollection()
  constructor() {
    if (!Cesium.defined(EditablePoint.baseColor)) {
      EditablePoint.pixelSize = EditablePoint.pixelSize ?? 6
      EditablePoint.baseColor = EditablePoint.baseColor ?? Cesium.Color.WHITE
      EditablePoint.highLightColor = EditablePoint.highLightColor ?? Cesium.Color.AQUA
      EditablePoint.outlineWidth = EditablePoint.outlineWidth ?? 1.0
      EditablePoint.outlineColor = EditablePoint.outlineColor ?? Cesium.Color.BLACK
    }
    this._postionChangedEvent = new Cesium.Event()
    this._removeEventSubscription = this._postionChangedEvent.addEventListener(
      this._onPostionChanged,
      this,
    )
  }

  set position(value) {
    if (!Cesium.defined(this._point)) {
      this._point = this._collection.add({
        id: this,
        color: EditablePoint.baseColor,
        outlineWidth: EditablePoint.outlineWidth,
        outlineColor: EditablePoint.outlineColor,
        pixelSize: EditablePoint.pixelSize,
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
      })
    }
    this._point.position = value
  }

  set extent(value) {
    this._extent = value
  }
  get isPrivate() {
    return this._private
  }
  set isPrivate(value) {
    this._private = value
  }

  get allowPick() {
    return this._allowPick
  }
  set allowPick(value) {
    this._allowPick = value
  }

  _onPostionChanged(
    entity,
    propertyName,
    newValue,
    oldValue,
  ) {
    if (propertyName === 'onPostionChanged' && Cesium.defined(this.onPostionChanged)) {
      this.onPostionChanged(
        entity,
        propertyName,
        newValue,
        oldValue,
      )
    }
  }

  updatePosition(position, tooltip) {
    if (!Cesium.defined(this._point)) {
      return
    }

    if (Cesium.defined(this._extent)) {
      const cartographic = Cesium.Cartographic.fromCartesian(position)

      if (cartographic.longitude < this._extent.west
        || cartographic.longitude > this._extent.east
        || cartographic.latitude < this._extent.south
        || cartographic.latitude > this._extent.north) {
        return
      }
    }
    this._point.position = position
    this._postionChangedEvent.raiseEvent(this, 'onPostionChanged', position, tooltip)

    if (Cesium.defined(tooltip)) {
      tooltip.onwer = this
      this._tooltip = tooltip
    }
  }

  onHighlight() {
    if (this._point) {
      this._point.color = EditablePoint.highLightColor
    }
  }

  offHighlight() {
    if (this._point) {
      this._point.color = EditablePoint.baseColor
    }
  }

  update(frameState) {
    if (!this._allowPick && frameState.passes.pick) {
      return
    }

    this._collection.update(frameState)
  }

  isDestroyed() {
    return false
  }

  destroy() {
    this._removeEventSubscription()
    this._collection = this._collection && this._collection.destroy()
    if (Cesium.defined(this._tooltip) && this._tooltip.onwer === this) {
      this._tooltip.visible = false
    }

    return Cesium.destroyObject(this)
  }
}


export default EditablePoint
